﻿package  
{
	import Box2D.Common.Math.b2Vec2;
	import flash.display.Sprite;
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	* @default Class to implement pick up items
	*/
	public class PickUp extends Sprite implements gameObject
	{
		public var position:b2Vec2;
		public var _animation:AnimatedSprite;
		private var bAlive:Boolean = true;
		private var bDead:Boolean = false;
		public function PickUp(X:Number,Y:Number) 
		{
			position = new b2Vec2(X, Y);
			
/*			this.graphics.beginFill(0xff0000);
			this.graphics.drawCircle(0, 0, 20);*/
			
			
		}
		public function initalise(X:Number, Y:Number):void
		{
			this.position.x = X;
			this.position.y = Y;
			
			
			
			this.x = this.position.x*30;
			this.y = this.position.y * 30;
			
			
		}
		
		public function Update():Boolean
		{
			this._animation.x = -this._animation.width / 2;
			this._animation.y = -this._animation.height / 2;
			this._animation.Update();
			
			if (checkCollision())
			{
				if (bAlive)
				{
					dispose();
					bAlive = false;
				
				}
				
			}

			if (bDead)
			{
				//this.die();
				return false;
			}
			return true;
		}
		
		private function checkCollision():Boolean
		{
			if(((World.playerPosition.x - World.playerWidth/2/30) < this.position.x + this.width / 2/30)
			&&((World.playerPosition.x) + World.playerWidth/2/30 > this.position.x - this.width / 2/30)
			&&((World.playerPosition.y - World.playerWidth/2/30) < this.position.y + this.height / 2/30)
			&&((World.playerPosition.y + World.playerWidth/2/30) > this.position.y - this.height / 2/30)
			  )
			  {
				  return true;
			  }
			  else
			  {
				  return false;
			  }
			
		}
		public function checkOnScreen():Boolean
		{
				if((!(this.position.x*30 > World.cameraPosition.x*30 
				&&this.position.x * 30 < World.cameraPosition.x * 30 + Constants.WIDTH))
				||
				(!(this.position.y*30 > World.cameraPosition.y*30 - Constants.HEIGHT*2 
				&&this.position.y * 30 < World.cameraPosition.y * 30)

				))
				{
					return false;
				}
				else
				{
					
					return true;
				}
		}
		public function CheckOnScreenx():Boolean
		{
				if(this.position.x*30 > World.cameraPosition.x*30 - Constants.WIDTH/2
				&&this.position.x * 30 < World.cameraPosition.x * 30 +Constants.WIDTH*1.5)
				
				

				
				{
					return false;
				}
				else
				{
					
					return true;
				}
		}
		public function CheckOnScreeny():Boolean
		{
			if(this.position.y*30 > World.cameraPosition.y*30
				&&this.position.y * 30 < World.cameraPosition.y * 30 + Constants.HEIGHT)
							{
					return false;
				}
				else
				{
					
					return true;
				}
		}
		
		public function dispose():void
		{
			//this needs to be pickup specific i.e apple, sandwhich, water
			Animation(this._animation.Animations[this._animation.CurrentAnimation]).bDying = true;
			
		}
		
		//function that will completely dispose of this pickup (any type) and remove it from it's parent's
		//display list
		public function die():void
		{
			this._animation.Dispose();
			this.removeChild(_animation);
			this.parent.removeChild(this);
			this.bDead = true;
		}
		
	}
	
}